import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Date;
import java.util.Calendar;


public class genInterface extends JFrame implements ActionListener
{
   private Container content5;

   private JLabel statusReq;
   private JLabel holidayStatus;
   private JTextField date = new JTextField(20);
   private JButton generateWeek = new JButton("Generate a 7 days timetable");
   private JButton back = new JButton("Go back");

   public genInterface()
   {
      content5 = getContentPane();
      setTitle("Generate Timetable");
      content5.setLayout(new GridLayout(0, 1));
      content5.add(new JLabel("Start date of the weekly timetable (dd/mm/yyyy)"));
      content5.add(date);
      content5.add(generateWeek);
      generateWeek.addActionListener(this);
      content5.add(back);
      back.addActionListener(this);
      // exit the program in case the window it's closed
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      pack();
   }//cont 

   private managerInterface first;
   public void actionPerformed(ActionEvent event)
   {
      
      if(event.getSource()==generateWeek)
      {
        String x = date.getText();
        if(x.equals(""))
	{
	  Object[] options = {"OK"};
          JOptionPane.showOptionDialog(null, "Start date can not be empty!",
          "Warning", JOptionPane.DEFAULT_OPTION,
          JOptionPane.WARNING_MESSAGE, null, options, options[0]); 
	  return;
	}
	
	DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date test_date1 = null;
      	try
	{
	  test_date1 = df.parse(x);
	}
	catch(ParseException error)
        {
	  Object[] options = {"OK"};
          JOptionPane.showOptionDialog(null, "The format of Start date or end date is wrong!",
          "Warning", JOptionPane.DEFAULT_OPTION,
          JOptionPane.WARNING_MESSAGE, null, options, options[0]); 
	  return;
	}
	if (!df.format(test_date1).equals(x))
	{
	  Object[] options = {"OK"};
          JOptionPane.showOptionDialog(null, "Start date is invalid day!",
          "Warning", JOptionPane.DEFAULT_OPTION,
          JOptionPane.WARNING_MESSAGE, null, options, options[0]); 
	  return;
	}

 	String[] words=date.getText().split("/");

	if(words.length<=3)
        {
	  try {
	    int day = Integer.parseInt(words[0]);
	    int month = Integer.parseInt(words[1])-1;
	    int year = Integer.parseInt(words[2]);
	    Date start = new GregorianCalendar(year,month,day).getTime();
            // Generate method
	    
	  }//try
          catch(NumberFormatException error)
          { 
	    Object[] options = {"OK"};
            JOptionPane.showOptionDialog(null, "Generate timetable failed!",
            "Warning", JOptionPane.DEFAULT_OPTION,
            JOptionPane.WARNING_MESSAGE, null, options, options[0]); 
            return;   
          }
        }//if	
      }//if reqHoliday
      if(event.getSource() == back)
      {
	    dispose();
        first = new managerInterface();
        first.setVisible(true);
      }
   } // actionPerformed

  
} // class
